package com.neuedu.nevserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.nevserver.mapper.FarmBatchMapper;
import com.neuedu.nevserver.mapper.SlauBatchMapper;
import com.neuedu.nevserver.mapper.WholBatchMapper;
import com.neuedu.nevserver.pojo.FarmBatch;
import com.neuedu.nevserver.pojo.ProductType;
import com.neuedu.nevserver.mapper.ProductTypeMapper;
import com.neuedu.nevserver.pojo.SlauBatch;
import com.neuedu.nevserver.pojo.WholBatch;
import com.neuedu.nevserver.service.ProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.nevserver.service.WholBatchService;
import com.neuedu.nevserver.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjy
 * @since 2025-03-18
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private FarmBatchMapper farmBatchMapper;

    @Autowired
    private SlauBatchMapper slauBatchMapper;

    @Autowired
    private WholBatchMapper wholBatchMapper;

    @Override
    public ResultVO<List<ProductType>> listProductByParent(Integer parentId) throws Exception{
        //查询父级产品
        if(parentId == null){
            QueryWrapper qw = new QueryWrapper<>();
            qw.isNull("parent_id");
            return ResultVO.success(null,productTypeMapper.selectList(qw));
        }
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("parent_id",parentId);
        return ResultVO.success(null,productTypeMapper.selectList(qw));
    }

    @Override
    public ResultVO<List<ProductType>> listProductByNode(Integer nodeId) throws Exception{
        List<ProductType> list = new ArrayList<>();
        //①根据流通节点编号nodeId查询批号信息列表获取到当前流通节点所有的产品类型编号
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("node_id",nodeId);
        qw.eq("state",2);

        //当前养殖企业可用的产品列表清单
        List<FarmBatch> farmBatchList = farmBatchMapper.selectList(qw);
        //获取养殖企业的产品类型编号
        Set<Integer> typeList = new HashSet<>();
        if(farmBatchList != null && farmBatchList.size() > 0){
            for(FarmBatch farmBatch : farmBatchList){
                typeList.add(farmBatch.getType());
            }
            //②根据查询到的产品类型编号查询产品类型列表信息
            return ResultVO.success(null,productTypeMapper.selectBatchIds(typeList));
        }

        //当前屠宰企业可用的产品列表清单
        List<SlauBatch> slaughterBatchList = slauBatchMapper.selectList(qw);
        //获取屠宰企业的产品类型编号
        Set<String> slauTypeList = new HashSet<>();
        if(slaughterBatchList != null && slaughterBatchList.size() > 0){
            for(SlauBatch slauBatch : slaughterBatchList){
                slauTypeList.add(slauBatch.getType());
            }
            //②根据查询到的产品类型编号查询产品类型列表信息
            return ResultVO.success(null,productTypeMapper.selectBatchIds(slauTypeList));
        }

        //当前批发商可用的产品列表清单
        List<WholBatch> wholBatchList = wholBatchMapper.selectList(qw);
        //获取批发商的产品类型编号
        Set<Integer> wholTypeList = new HashSet<>();
        if(wholBatchList != null && wholBatchList.size() > 0){
            for(WholBatch wholBatch : wholBatchList){
                wholTypeList.add(wholBatch.getType());
            }
            //②根据查询到的产品类型编号查询产品类型列表信息
            return ResultVO.success(null,productTypeMapper.selectBatchIds(wholTypeList));
        }

        return ResultVO.success(null,list);
    }
}
